﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Zhjt.Comm.Lib.Tools.WebTools
{
    public enum ValidateCodeType
    {
        /// <summary>
        /// 字符数字混合
        /// </summary>
        Mingle = 0,
        /// <summary>
        /// 数字
        /// </summary>
        Number = 1,
        /// <summary>
        /// 字符
        /// </summary>
        Letter = 2

    }
    public class ImageValidateCodeBuilderTool
    {
        /// <summary>
        /// 返回验证码的字符串
        /// </summary>
        /// <param name="validateCodeType">验证码字符串类型 </param>
        /// <param name="validateCodeCount">验证码字符个数</param>
        /// <param name="character">验证码字符库</param>
        /// <returns></returns>
        public string GetValidateCode(ValidateCodeType validateCodeType, int validateCodeCount, char[] character)
        {
            char code;  //用于存放生成验证码的单个字符
            StringBuilder checkCode = new StringBuilder();
            System.Random random = new Random();

            for (int i = 0; i < validateCodeCount; i++)
            {
                code = character[random.Next(character.Length)];
                switch (validateCodeType)
                {
                    case ValidateCodeType.Number:
                        if (code < 48 || code > 57)
                        {
                            i--;
                            continue;
                        }
                        break;
                    case ValidateCodeType.Letter:
                        if (code < 65 || code > 90)
                        {
                            i--;
                            continue;
                        }
                        break;
                }
                checkCode.Append(code);
            }
            return checkCode.ToString();
        }

        /// <summary>
        ///根据字符生成图片,并且返回对应内存流
        /// </summary>
        /// <param name="code">字符</param>
        /// <param name="width">图片宽</param>
        /// <param name="height">图片高</param>
        /// <returns></returns>
        public byte[] CreateCheckCodeImage(string code, int width, int height)
        {
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);

            try
            {
                //生成随机生成器  
                Random random = new Random();
                //清空图片背景色  
                g.Clear(System.Drawing.Color.White);
                //画图片的背景噪音线  
                for (int i = 0; i < 50; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Silver), x1, y1, x2,

        y2);
                }

                System.Drawing.Font font = new System.Drawing.Font("Arial", 14,

        (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new

        System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0,

        image.Width, image.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f,

        true);

                int cySpace = 16;
                int validateCodeCount = code.Length;
                for (int i = 0; i < validateCodeCount; i++)
                {
                    //   g.DrawString(checkCode.Substring(i, 1), font, brush, (i + 1) * cySpace, 1);
                    g.DrawString(code[i].ToString(), font, brush, (i + 1) * cySpace, 1);
                }

                //画图片的前景噪音点  
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);

                    image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next()));
                }

                //画图片的边框线  
                g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Silver), 0, 0,

        image.Width - 1, image.Height - 1);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();

                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                return ms.ToArray();


            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

        /// <summary>
        /// 返回验证码图片的byte数组
        /// </summary>
        /// <param name="validateCodeType"></param>
        /// <param name="validateCodeCount">验证码字符串长度</param>
        /// <param name="character"></param>
        /// <param name="outCode"></param>
        /// <returns></returns>
        public byte[] CreateDefaultCheckCodeImage(ValidateCodeType validateCodeType, int validateCodeCount, char[] character, out string outCode)
        {
            outCode = GetValidateCode(validateCodeType, validateCodeCount, character);
            int width = (int)(outCode.Length * 15.0 + 40);
            return CreateCheckCodeImage(outCode, width, 23);
        }


        /// <summary>
        /// 生成一个默认的验证码图片,并加入上下文的输出流中
        /// </summary>
        /// <param name="validateCodeType"></param>
        /// <param name="validateCodeCount">验证码字符串长度</param>
        /// <param name="character">验证码字符库</param>
        /// <param name="context">上下文</param>
        /// <param name="outCode">反出的验证码字符</param>
        public void CreateDefaultCheckCodeImage(ValidateCodeType validateCodeType, int validateCodeCount, char[] character, HttpContext context, out string outCode)
        {
            outCode = GetValidateCode(validateCodeType, validateCodeCount, character);
            int width = (int)(outCode.Length * 15.0 + 40);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            byte[] bitmap = CreateCheckCodeImage(outCode, width, 23); ;

            context.Response.ClearContent();
            context.Response.ContentType = "image/Gif";
            context.Response.BinaryWrite(bitmap);
        }

    }
}
